package com.ruoyi.web.controller.mes;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.web.domain.mes.MesMachineRelevance;
import com.ruoyi.web.domain.mes.MesOrderProduct;
import com.ruoyi.web.service.MesMachineRelevanceService;
import com.ruoyi.web.vo.Produce;
import com.ruoyi.web.vo.ProduceProduct;
import com.ruoyi.web.vo.Report;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/MachineRelevance")
public class MesMachineRelevanceController extends BaseController {

    @Resource
    private MesMachineRelevanceService mesMachineRelevanceService;

    //查询list
    @PostMapping(value = "/list")
    public TableDataInfo list(@RequestBody Map<String, String> params) {
        startPage(params);
        String printMachineName = params.get("printMachineName");
        String foldMachineName = params.get("foldMachineName");
        TableDataInfo data = getDataTable(mesMachineRelevanceService.list(new QueryWrapper<MesMachineRelevance>().lambda()
                .like(StringUtils.isNotEmpty(printMachineName),MesMachineRelevance::getPrintMachineName,printMachineName)
                .like(StringUtils.isNotEmpty(foldMachineName),MesMachineRelevance::getFoldMachineName,foldMachineName)
                .orderByDesc(MesMachineRelevance::getId)));
        return data;
    }
    //根据印刷机台查询折页机台
    @PostMapping(value = "/getPrintMachineId")
    public TableDataInfo getPrintMachineId(@RequestBody Map<String, String> params) {
        String printMachineId = params.get("printMachineId");
        TableDataInfo data = getDataTable(mesMachineRelevanceService.list(new QueryWrapper<MesMachineRelevance>().lambda()
                .eq(MesMachineRelevance::getPrintMachineId,printMachineId)));
        return data;
    }

    //新增或编辑
    @PostMapping(value = "/add")
    @Transactional
    public AjaxResult add(@RequestBody MesMachineRelevance mesMachineRelevance) {

      List<MesMachineRelevance> data=  mesMachineRelevanceService.list(new QueryWrapper<MesMachineRelevance>().lambda()
              .eq(MesMachineRelevance::getPrintMachineId, mesMachineRelevance.getPrintMachineId())
              .eq(MesMachineRelevance::getFoldMachineId, mesMachineRelevance.getFoldMachineId()));

        if (data!=null&&data.size()!=0){
            throw new ServiceException(data.get(0).getPrintMachineName()+ "已存在！");
        }
        mesMachineRelevance.setId(UUID.randomUUID().toString().replace("-", ""));
         mesMachineRelevanceService.saveOrUpdate(mesMachineRelevance);
         return AjaxResult.success("添加成功", mesMachineRelevance.getId());
        }
    @PostMapping(value = "/update")
    @Transactional
    public AjaxResult update(@RequestBody MesMachineRelevance mesMachineRelevance) {
        mesMachineRelevanceService.update(new UpdateWrapper<MesMachineRelevance>().lambda()
                .set(MesMachineRelevance::getIsEnable, mesMachineRelevance.getIsEnable()).eq(MesMachineRelevance::getId, mesMachineRelevance.getId()));

        return AjaxResult.success("修改成功", mesMachineRelevance.getId());
    }
        //删除
    @PostMapping(value = "/deleteById")
    public AjaxResult deleteById(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        mesMachineRelevanceService.removeById(id);
        return AjaxResult.success("删除成功");
    }
}
